package cn.zmit.kuxi.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.entity.JoinRecordEntity;
import cn.zmit.kuxi.holder.JoinRecordHolder;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;

/** * @author chenxu @parameter * @return */
public class Joinrecord extends BaseActivity {
	@ViewInject(R.id.list_goods)
	private ListView list_goods;
	private ListViewDataAdapter<JoinRecordEntity> joinrecordAdapter;
	public String userId = null, flag = null;
	/**
	 * 页码
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleView("参与记录", true);
		setContentView(R.layout.fragment_goods);
		ViewUtils.inject(this);
		initView();
		// flag=getTextFromBundle("flag");
		// if (flag.equals("1")) {
		// String winId=getTextFromBundle("win_id");
		// RequestTask.getInstance().getHistoryRecord(this, winId,new
		// onRequestListener());
		//
		userId = getTextFromBundle("userId");
		PreferenceHelper.write(context, SPConstants.SHARED_PREFERENCE_NAME,
				SPConstants.WINNERID, userId);
		getDate();
		initLoadMore();
	

	}

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mLoadMoreListViewContainer.useDefaultFooter(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
				}
				LogUtils.i("加载更多--->page:" + mPage);
				getDate();
			}
		});
	}

	private void getDate() {
		RequestTask.getInstance().getHistoryRecord(this, userId, mPage + "",
				new onRequestListener());
	}

	private void initView() {
		joinrecordAdapter = new ListViewDataAdapter<JoinRecordEntity>();
		joinrecordAdapter.setViewHolderClass(this, JoinRecordHolder.class);
		list_goods.setAdapter(joinrecordAdapter);
	}

	// 参与记录
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载
			JsonData jsonData = JsonData.create(response);
			JsonData data = jsonData.optJson("winner_buy_record");
			if (data != null && data.length() > 0) {
				if (data.length() > 0) {
					if (data.length() == 8) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 8) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					for (int i = 0; i < data.length(); i++) {
						JsonData itemData = data.optJson(i);
						String buy_count = itemData.optString("buy_count");
						String all_buy_count = itemData.optString("all_buy_count");
						String price = itemData.optString("price");
						String name = itemData.optString("name");
						String title = itemData.optString("phase");
						String winner_id = itemData.optString("winner_id");
						String time = itemData.optString("announceOn");
						String lucky_number = itemData
								.optString("lucky_number");
						String pic = itemData.optString("pic");
						JoinRecordEntity joinRecordEntity = new JoinRecordEntity(
								buy_count, name, title, winner_id, time,
								lucky_number, pic,price,all_buy_count);
						joinrecordAdapter.append(joinRecordEntity);
					}
				}

			} else {
				mIsEmpty = true; // 空数据
				mHasMore = false; // 无更多数据
			}
			mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); // 判断是否加载更多
		}

		@Override
		public void onRequestPrepare() {
			// TODO Auto-generated method stub
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			// TODO Auto-generated method stub
			super.onRequestFailed(error);
		}
	}
}
